<?php
/**
 * 全局配制文件接口文件
 * 
 * 所有与环境相关的配制都集中放在一个配制文件中
 * config_dev.php 开发环境的配制文件
 * config_release.php 线上环境的配制文件
 * config_test.php 测试环境的配制文件
 * 
 * 开发、测试、线上环境此文件引入的配制文件是不同，
 * 此文件布署到对应的环境中后，一般不需要修改
 */

//
//根据实际环境修改此处包含!!!!
//
$__env_files = array(
    'DEV'    => 'dev',
    'TEST'   => 'test',
    'ONLINE' => 'release',
);
if (!defined('__OA_ENV')) {
    $hostname = php_uname('n');
    $devHostnames = array('oa-dev', 'localhost.localdomain', 'oa3web05');
    $testHostnames = array('C113479', 'C113464', 'C113480', 'fefe', 'open_test', 'oa3app03');
    $onlineHostnames = array('oa3web01', 'oa3web02', 'oa3web03');
    if (in_array($hostname, $devHostnames)) {
        define('__OA_ENV', 'DEV');
		define("__UC_ENV" , 'DEV');
    } elseif (in_array($hostname, $testHostnames)) {
        define('__OA_ENV', 'TEST');
		define("__UC_ENV" , 'TEST');
    } elseif (in_array($hostname, $onlineHostnames)) {
        define('__OA_ENV', 'ONLINE');
        define('__UC_ENV', 'ONLINE');
    } else{
    	define('__OA_ENV', 'DEV');
    	define('__UC_ENV', 'DEV');
    }
}
$__env_file = $__env_files[__OA_ENV]; 

require dirname(__FILE__)."/config_{$__env_file}.php";

class Oa_Util_Config
{
    /**
     * 获取一个配制值
     *
     * @param string $key 配制名
     * @param string $subKey default NULL,子配制项名
     * @param string $subSubKey default NULL, 子子配制项名
     * @return mixed
     */
    public static function getConfig ($key, $subKey = NULL, $subSubKey = NULL)
    {
        $val = $GLOBALS['gConfig'][$key];
        
        if ($subKey === NULL) {
            return $val;
        }
        
        $val = $val[$subKey];
        
        if ($subSubKey === NULL) {
            return $val;
        }
        
        return $val[$subSubKey];
    }
    
    /**
     * 获取单个DB配置
     * 返回如下配置：
     * array(
     *   'host' => ... ip
     *   'port' => ... 端口
     *   'user' => ... 用户名
     *   'pass' => ... 密码
     *   'name' => ... 数据库名
     * )
     * @param int $clusterId DB Cluster ID
     * @param boolean $read 是否只读
     * @return array|boolean
     */
    public static function getSingleDbConfig($clusterId, $read = false)
    {
        if (!isset($GLOBALS['gConfig']['db_singles'][$clusterId])) {
            return false;
        }
        $logicalConfig = $GLOBALS['gConfig']['db_singles'][$clusterId];
        $physicalConfig = $GLOBALS['gConfig']['db_physical'][$logicalConfig['map']];
        if ($read == true) {
            $host = $physicalConfig['read']['host'];
            $port = $physicalConfig['read']['port'];
            if (isset($physicalConfig['read']['db_user'])) {
                $user = $physicalConfig['read']['db_user'];
            } else if (isset($physicalConfig['db_user'])) {
                $user = $physicalConfig['db_user'];
            } else {
                $user = $GLOBALS['gConfig']['db_physical']['db_user'];
            }
            if (isset($physicalConfig['read']['db_pwd'])) {
                $pass = $physicalConfig['read']['db_pwd'];
            } else if (isset($physicalConfig['db_pwd'])) {
                $pass = $physicalConfig['db_pwd'];
            } else {
                $pass = $GLOBALS['gConfig']['db_physical']['db_pwd'];
            }
        } else {
            $host = $physicalConfig['write']['host'];
            $port = $physicalConfig['write']['port'];
            if (isset($physicalConfig['write']['db_user'])) {
                $user = $physicalConfig['write']['db_user'];
            } else if (isset($physicalConfig['db_user'])) {
                $user = $physicalConfig['db_user'];
            } else {
                $user = $GLOBALS['gConfig']['db_physical']['db_user'];
            }
            if (isset($physicalConfig['write']['db_pwd'])) {
                $pass = $physicalConfig['write']['db_pwd'];
            } else if (isset($physicalConfig['db_pwd'])) {
                $pass = $physicalConfig['db_pwd'];
            } else {
                $pass = $GLOBALS['gConfig']['db_physical']['db_pwd'];
            }
        }
        return array(
            'host' => $host,
            'port' => $port,
            'user' => $user,
            'pass' => $pass,
            'name' => $logicalConfig['db_name']
        );
    }
}
